<template>
  <el-drawer :visible.sync="visible" :title="title" :modal-append-to-body="true" :append-to-body="true"
    :destroy-on-close="false" :modal="true" direction="rtl" :wrapperClosable="false" custom-class="form-drawer"
    size="450px" @closed="closed">

    <div class="form-drawer__content">
      <el-form ref="formData" :model="formData" :rules="rules" style="padding: 20px;">
        <el-form-item :label-width="formLabelWidth" label="图层名称" prop="name">
          <el-input v-model="formData.name" placeholder="图层名称" autocomplete="off" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="图层类型" prop="type">
          <el-select v-model="formData.type" placeholder="请选择" style="width: 100%;">
            <el-option label="POI点" value="POI"></el-option>
            <el-option label="路径" value="PATH"></el-option>
            <el-option label="区域" value="RANGE"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="序号" prop="sort">
          <el-input-number v-model="formData.sort" :min="0" :max="1000" label="请输入序号" style="width: 100%;" />
        </el-form-item>
      </el-form>

      <div class="form-drawer__footer">
        <el-button @click="closed" :loading="loading">取 消</el-button>
        <el-button type="primary" :loading="loading" @click="submitForm">确 定</el-button>
      </div>
    </div>

  </el-drawer>
</template>

<script>
  import {
    saveLayer,
  } from '@/api/subsystem/layer'
  export default {
    name: 'InfoDialog',
    components: {},
    props: {
      properties: {
        type: Object,
        default: () => ({})
      }
    },
    computed: {},
    watch: {
      properties: {
        handler: function(value) {
          this.visible = value.visible
          if (value.data) {
            this.formData.id = value.data.id || ''
            this.formData.projectId = value.data.projectId || this.$store.getters.projectId
            this.formData.name = value.data.name || ''
            this.formData.type = value.data.type || 'POI'
            this.formData.identify = value.data.identify || ''
            this.formData.sort = value.data.sort || 0
          } else {
            this.formData.id = ''
            this.formData.projectId = this.$store.getters.projectId
            this.formData.name = ''
            this.formData.type = 'POI'
            this.formData.identify = ''
            this.formData.sort = 0
          }
          if (value.type) {
            this.type = value.type
            if (value.type === '0') {
              this.title = '新建图层'
            }
            if (value.type === '1') {
              this.title = '编辑图层'
            }
          }
        },
        immediate: true,
        deep: false
      }
    },
    data() {
      return {
        visible: false,
        title: '',
        loading: false,
        type: '',
        formData: {
          id: '',
          projectId: this.$store.getters.projectId,
          name: '',
          type: '',
          identify: '',
          sort: 0,
        },
        formLabelWidth: '100px',
        rules: {
          name: [{
              required: true,
              message: '请输名称',
              trigger: 'blur'
            },
            {
              min: 1,
              max: 50,
              message: '长度在 2 到 50 个字符',
              trigger: 'blur'
            }
          ],
        }
      }
    },
    created() {
    },
    methods: {
      submitForm: function() {
        this.$refs.formData.validate((valid) => {
          if (valid) {
            this.loading = true
            saveLayer(this.formData).then(rsp => {
                if (200 == rsp.code) {
                  this.$message({
                    type: 'success',
                    message: rsp.message
                  })
                }
                this.closed()
              }).catch(err => {
                console.log(err);
              })
              .finally(() => {
                this.loading = false
              })
          }
        })
      },
      closed: function() {
        this.$emit('closed', false)
      },
    }
  }
</script>


<style lang="scss">
</style>
